package com.zimmur.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zimmur.common.ReturnJsonEntity;
import com.zimmur.common.StatusCodeEnum;
import com.zimmur.model.goods.WishListParam;
import com.zimmur.model.goods.WishListResParam;
import com.zimmur.service.order.IWishListService;
import com.zimmur.tools.PageUtil;

/**
* <p>Title: WishListController</p>
* <p>Description: 愿望清单</p>
* <p>Company: zimmur</p>
* <p>Copyright: Copyright (c) 2017</p>
* @author huangsx
* @version 1.0
* @time 2018年1月10日 下午7:39:58
*/
@RestController
@RequestMapping("/restful/wishlist")
public class WishListController extends BaseController{
	private static Logger log = LoggerFactory.getLogger(WishListController.class);
	@Resource
	private IWishListService wishListService;
	
	@Value("#{spingMvcProperties['System.GuestId']}")
	private String guessUserId;
	
	//加入愿望清单
	@RequestMapping(value="/add",method = RequestMethod.POST)
	public ReturnJsonEntity addWishList(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		Map<String, Object> map=new HashMap<>();
		try{
			//游客身份验证
			if(String.valueOf(getTokenUserId()).equals(guessUserId)){
				statusCodeEnum=StatusCodeEnum.CODE100200;
				entity.setCode(statusCodeEnum.getCode());
				entity.setMsg(statusCodeEnum.getMsg());
				return entity;
			}
			long userId = getTokenUserId();
			long goodsId = getLongParam("goodsId");
			String  resource  = getParam("resource");
			if(wishListService.addWishList(goodsId, userId,resource)){
				statusCodeEnum = StatusCodeEnum.CODE000000;
			}
		}catch(Exception er){
			log.error(er.getMessage());
		}
		entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		entity.setData(map);
		return entity;
	}
	/**
	* <p>Title: moveToCart</p>
	* <p>Description:愿望单添加到购物车  </p>
	* @return
	 */
	@RequestMapping(value="/movetocart",method = RequestMethod.GET)
	public ReturnJsonEntity moveToCart(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		Map<String, Object> map=new HashMap<>();
		try{
			//游客身份验证
			if(String.valueOf(getTokenUserId()).equals(guessUserId)){
				statusCodeEnum=StatusCodeEnum.CODE100200;
				entity.setCode(statusCodeEnum.getCode());
				entity.setMsg(statusCodeEnum.getMsg());
				return entity;
			}
			
			long userId = getTokenUserId();
			long goodsId = getLongParam("goodsId");
			long sizeId = getLongParam("sizeId");
			Integer quantity = getIntParam("quantity");
			Integer isPrepay = getIntParam("isPrepay");
			if(wishListService.MoveToCart(userId,goodsId,sizeId,quantity,isPrepay)){
				statusCodeEnum = StatusCodeEnum.CODE000000;
			}
		}catch(Exception er){
			log.error(er.getMessage());
		}
		entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		entity.setData(map);
		return entity;
	}

	/**
	* <p>Title: delete</p>
	* <p>Description:删除愿望单 </p>
	* @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.GET)
	public ReturnJsonEntity delete() {
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		Map<String, Object> map=new HashMap<>();
		try {
			//游客身份验证
			if(String.valueOf(getTokenUserId()).equals(guessUserId)){
				statusCodeEnum=StatusCodeEnum.CODE100200;
				entity.setCode(statusCodeEnum.getCode());
				entity.setMsg(statusCodeEnum.getMsg());
				return entity;
			}
		
		    long userId = getTokenUserId();
			long goodsId = getLongParam("goodsId");
			if(wishListService.delete(userId,goodsId)){
				statusCodeEnum = StatusCodeEnum.CODE000000;
			}
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception er) {
			log.error(er.getMessage());
		}
		entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		entity.setData(map);
		return entity;
	}
	/**
	* <p>Title: queryWishListByUser</p>
	* <p>Description: 愿望单列表</p>
	* @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ReturnJsonEntity queryWishListByUser() {
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;

		try {
			//游客身份验证
			if(String.valueOf(getTokenUserId()).equals(guessUserId)){
				statusCodeEnum=StatusCodeEnum.CODE100200;
				entity.setCode(statusCodeEnum.getCode());
				entity.setMsg(statusCodeEnum.getMsg());
				return entity;
			}
			
			long userId = getTokenUserId();
			WishListParam param = new WishListParam();
			Integer pageNow = getIntParam("pageNow");
			//Integer pageSize = getParam("pageSize",10);
			String orderByClause = "";
			// 排序条件
			String order= getParam("order");
			String clause=getParam("clause");
			param.setPageIndex(pageNow);
			//param.setPageSize(pageSize);
			param.setUserId(userId);
			if(order!=null && !order.isEmpty() 
					&& clause!=null && !clause.isEmpty()&&order.equals("asc")){
				orderByClause = "a.createDate asc";
			}else{
				orderByClause = "a.createDate desc";
			}
			param.setOrderByClause(orderByClause);
			PageUtil<WishListResParam> pageUtil = wishListService.queryByUser(param);
			if(pageUtil!=null){
				entity.setData(pageUtil);
			}else{
				entity.setData(new ArrayList<>());
			}
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception er) {
			log.error(er.getMessage());
			entity.setData(new ArrayList<>());
		}
		entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());

		return entity;
	}
	
	
}